$(function () {
    findAllMessage();
})

function updataDate() {
    var data = {
        id: recipient,
        message: $("#umessage").val(),
        status: $("#ustatus option:selected").val()
    }
    $.ajax({
        url: "/mall/update_message",
        type: "POST",
        data: data,
        dataType: "json",
        success: function (json) {
            if (json.code = 200) {
                alert("更新成功");
                findAllMessage();
                $("#exampleModal").modal('hide');
            } else {
                alert("更新失败")
                $("#exampleModal").modal('hide');
            }
        }
    })
}

function InsertDate() {
    var data = {
        message: document.getElementById("imessage").value,
        status: $("#istatus option:selected").val()
    }
    $.ajax({
        url: "/mall/add_message",
        type: "POST",
        data: data,
        dataType: "json",
        success: function (json) {
            if (json.code = 200) {
                alert("添加成功");
                findAllMessage();
                $("#exampleModalInsert").modal('hide');
            } else {
                alert("添加失败");
                $("#exampleModalInsert").modal('hide');
            }
        }
    })
}

function del(item) {
    $.ajax({
        url: "/mall/delete_message?messId=" + item,
        type: "GET",
        dataType: "json",
        success: function (json) {
            alert("删除成功");
            findAllMessage();

        }, error: function () {
            alert("删除失败");
        }

    })
}

var findAllMessage = function () {
    $.ajax({
        url: "/mall/message",
        type: "GET",
        dataType: "json",
        success: function (json) {
            var data = json.data;
            var str = "";
            for (var i = 0; i < data.length; i++) {
                var status = "禁用";
                if (validate(data[i].status) == 1) {
                    status = "已发布";
                }
                str += '<div class="panel panel-default">' +
                    '<div class="panel-heading">' +
                    ' <span style="margin-right: 15px">创建时间：'+formatDate(data[i].gtmUpdate)+'</span><span style="margin-right: 15px">创建者：'+data[i].createrId+'</span><span >状态：'+status+'</span>' +
                    '<span><button style="float: right" class="btn btn-success" type="button" data-target="#exampleModal" data-toggle="modal"' +
                    'data-whatever="' + data[i].id + '">消息编辑</button></span>' +
                    '<span><button style="float: right" class="btn btn-danger" type="button" onclick="del('+data[i].id+')">删除消息</button></span>' +
                    '</div>'+
                    '<div class="panel-body">'+
                     data[i].message+
                    '</div>' +
                    '</div>';
            }
            $('#messageShow').html(str);
        }, error: function () {
            alert("flase");
        }
    })
}
var validate = function (data) {
    if (data == null) {
        return "";
    }
    return data;
}
//时间戳转系统时间格式
function formatDate(now) {
    if (now==""){return "";}
    return new Date(now).toLocaleString();
}